package rpcserver

import (
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/logsvc"
	"gluenet/utils/rpc/v2/define"
)

const (
	log = rpcv1.RPC_SERVICE_LOG
)

func init() {
	depend.Provide(NewLogRouter)
	registerRouter(log, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *logRouter) {
			d.initRouter(c)
		})
	})
}

type logRouter struct {
	log *logsvc.Service
}

func NewLogRouter(svc *logsvc.Service) *logRouter {
	return &logRouter{svc}
}

func (r *logRouter) initRouter(c rpcserver.IRoutes) {
	c.Group(`log`).
		Func(`counts`, r.logCounts).
		Func(`info`, r.logInfo)
}

func (r *logRouter) logCounts(c *rpcserver.Context) {
	logCountsReq := new(define.LogCounts)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{logCountsReq}); err != nil {
		return
	}
	resp := r.log.LogCounts(*logCountsReq)
	c.JSON(resp)
}

func (r *logRouter) logInfo(c *rpcserver.Context) {
	logInfoReq := new(define.LogInfo)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{logInfoReq}); err != nil {
		return
	}
	resp := r.log.LogInfo(*logInfoReq)
	c.JSON(resp)
}
